package org.jetlinks.community.mstn;

import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.param.Term;
import org.hswebframework.utils.StringUtils;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.Authentication;
import org.hswebframework.web.authorization.Dimension;
import org.hswebframework.web.authorization.Permission;
import org.jetlinks.community.mstn.entity.MstnMenuAssetAccess;
import org.jetlinks.community.mstn.exception.MstnAssetsUnknownException;
import org.jetlinks.community.mstn.logging.MstnAssetsRequestCtx;
import org.jetlinks.community.mstn.term.MstnTenantAssetsTermBuilder;
import org.jetlinks.community.mstn.term.MstnTenantDimensionTermBuilder;
import org.jetlinks.community.mstn.term.MstnTenantUserTermBuilder;
import org.jetlinks.core.enums.ErrorCode;
import reactor.core.publisher.Mono;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Getter
@Setter
public class MstnAssetsHolder {

    private MstnMenuAssetAccess assetAccess;

    private String targetId;

    private String type;

    public static MstnAssetsHolder check(Authentication authentication, MstnAssetsRequestCtx requestCtx ,String type){

        //管理员
        if ("admin".equals(authentication.getUser().getUsername())){
            return new MstnAssetsHolder();
        }

        String  tenantId = "";
        for (Dimension dimension: authentication.getDimensions()) {
            if ("tenant".equals(dimension.getType().getId())){
                tenantId = dimension.getId();
                break;
            }
        }


        if (StringUtils.isNullOrEmpty(tenantId)) {
            throw new MstnAssetsUnknownException(ErrorCode.UNKNOWN, "无法获取到的资产类型");
        }
        MstnAssetsHolder holder = new MstnAssetsHolder();
        holder.setType(type);
        requestCtx.setTenantId(tenantId);
        requestCtx.putContext("userId", authentication.getUser().getId());
        requestCtx.putContext("username", authentication.getUser().getUsername());
        requestCtx.putContext("userName", authentication.getUser().getName());


        if (authentication.getAttribute("mstn-assets-" + type).isPresent()) {
            MstnMenuAssetAccess assetAccess = (MstnMenuAssetAccess)authentication.getAttribute("mstn-assets-" + type).get();
            holder.setAssetAccess(assetAccess);
            //自己创建
            if ("creator".equals(assetAccess.getSupportId())){
                holder.setTargetId(authentication.getUser().getId());
            } else if ("org".equals(assetAccess.getSupportId())) {//机构下
                Serializable orgSerializable = authentication.getAttribute("default-org").get();
                holder.setTargetId((String) orgSerializable);
            } else if ("org-include-children".equals(assetAccess.getSupportId())) { //所在组织及下级组织
                holder.setTargetId(tenantId);
            } else { //全部数据

            }
        }
        return holder;
    }

    /**
     * 注入sql
     * @return
     */
    public Term injectQuery(){
        if (assetAccess == null) {
            return null;
        }
        //全部数据
        if ("ignore".equals(assetAccess.getSupportId())){

        } else {
            Term term = new Term();
            term.setColumn("id");
            if ("tenant".equals(getType())) {
                term.setTermType(MstnTenantDimensionTermBuilder.tenantDimension + "$" + assetAccess.getSupportId() + "$" + "tenant");
            } else if ("organization".equals(getType())) {
                term.setTermType(MstnTenantDimensionTermBuilder.tenantDimension + "$" + assetAccess.getSupportId() + "$" + "org" );
            } else {
                term.setTermType(MstnTenantAssetsTermBuilder.tenantAssets + "$" + assetAccess.getSupportId());
            }
            term.setValue(targetId);
            return term;
        }
        return null;
    }
}
